//
//  EFPageCollectionMenuCell.m
//  ESPTablet
//
//  Created by Javor on 14-8-12.
//  Copyright (c) 2014年 Javor. All rights reserved.
//

#import "EFPageCollectionMenuCell.h"
//#ifdef NEED_FRAMEWORK
//#import <IOSESPBaseFramework/FileUtil.h>
//#else
#import "FileUtil.h"
//#endif
#import "EnvironmentVariable.h"
#import "ThemeKit.h"

@interface EFPageCollectionMenuCell()
{
    UIImageView *_icon;
    UILabel *_label;
    UILabel *_indicateLabel;//右上角提示
}

@end

@implementation EFPageCollectionMenuCell
@synthesize imagePath = _imagePath, title = _title;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _icon = [[UIImageView alloc] init];
        _label = [[UILabel alloc] init];
        _indicateLabel = [[UILabel alloc] init];
        self.layer.borderColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1].CGColor;
        self.layer.borderWidth = 0.5;
        [self.contentView addSubview:_icon];
        [self.contentView addSubview:_label];
        [self.contentView addSubview:_indicateLabel];
        [self createTheme];
    }
    return self;
}

-(void)createTheme {
     self.contentView.theme_backgroundColor = self.theme_backgroundColor = Theme_BackgroudColor;
    _label.theme_textColor = Theme_TitleColor;
    self.layer.theme_borderColor = Theme_LineCGColor;
}

-(void) creationComplete{
    [self changeCellWithImageWidth:_imageWidth imageHeight:_imageHeight titleHeight:_titleHeight];
    
    _label.textAlignment = NSTextAlignmentCenter;
    
    _indicateLabel.textAlignment = NSTextAlignmentCenter;
    _indicateLabel.textColor = [UIColor whiteColor];
    _indicateLabel.font = [UIFont systemFontOfSize:_indicateLabel.frame.size.width/3*2];
    _indicateLabel.layer.backgroundColor = [UIColor redColor].CGColor;
    
}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    _icon.layer.shadowColor = [UIColor yellowColor].CGColor;
//    _icon.layer.shadowOffset = CGSizeMake(0.5, 0.5);
//    _icon.layer.shadowOpacity = 0.5;
//    _icon.layer.shadowRadius = 6.0;
//    return YES;
//}
//
//-(void)iconTouchDown{
//    _icon.layer.shadowColor = [UIColor yellowColor].CGColor;
//    _icon.layer.shadowOffset = CGSizeMake(0.5, 0.5);
//    _icon.layer.shadowOpacity = 0.5;
//    _icon.layer.shadowRadius = 6.0;
//}
//
//-(void)iconTouchUp{
//    _icon.layer.shadowOpacity = 0;
//}


-(void)setImagePath:(NSString *)aImagePath{
    _imagePath = aImagePath;
    if(![_imagePath isEqualToString:@""]){
        NSString *path = [[EnvironmentVariable getVersionPath] stringByAppendingString:@"/Image"];
        _image = [[UIImage alloc]initWithData:[FileUtil getFileDataWithPath:_imagePath andTypePath:path]];
        //        UIImage *iconImage = [UIImage imageWithContentsOfFile:_imagePath];
    }
    _icon.image = _image;
}

-(void)setImage:(UIImage *)image{
    _image = image;
    _icon.image = _image;
}

-(void)setTitle:(NSString *)aTitle{
    _title = aTitle;
    _label.text = _title;
}

-(void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    _label.textColor = titleColor;
    _label.theme_textColor =[UIColor theme_colorForKey:@"cellTitle" ];
}

-(void)setTitleFontSize:(CGFloat)titleFontSize{
    _titleFontSize = titleFontSize;
    _label.font = [UIFont systemFontOfSize:titleFontSize];
}

-(void)setIndicateLabelHidden:(BOOL)indicateLabelHidden{
    _indicateLabelHidden = indicateLabelHidden;
    _indicateLabel.hidden = indicateLabelHidden;
}

-(void) setIndicateCount:(int)indicateCount{
    _indicateCount = indicateCount;
    if (indicateCount == 0) {
        self.indicateLabelHidden = YES;
    }else{
        self.indicateLabelHidden = NO;
        
    }
    _indicateLabel.text = [NSString stringWithFormat:@"%d",indicateCount];
}

-(void)changeCellWithImageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight titleHeight:(CGFloat)titleHeight{
    //_icon 和 _label的间距
    int space = 0;
    //    _icon.center = _icon.superview.center;
    //    CGRect superviewframe = _icon.superview.frame;
    CGRect selfFrame = self.frame;
    _icon.frame = CGRectMake((selfFrame.size.width - imageWidth)/2, (selfFrame.size.height - imageHeight - space - titleHeight)/2, imageWidth, imageHeight);
    _label.frame = CGRectMake(0, _icon.frame.origin.y +_icon.frame.size.height+space, _label.superview.frame.size.width, titleHeight);
    _indicateLabel.frame = CGRectMake(_icon.frame.origin.x + _icon.frame.size.width - imageWidth/3, _icon.frame.origin.y, imageWidth/3, imageHeight/3);
    _indicateLabel.layer.cornerRadius = _indicateLabel.frame.size.width/2;
    //    NSLog(@"右上角指示弧度：%f",_indicateLabel.frame.size.width/2);
}

//未读数量加一
- (void)badgePlusOne{
    NSString *tagValue = _indicateLabel.text;
    NSString *newTagValue = [NSString stringWithFormat:@"%ld",([tagValue integerValue]+1)];
    _indicateLabel.text = newTagValue;
}

//未读数量减一
- (void)badgeMinusOne{
    NSString *tagValue = _indicateLabel.text;
    NSString *newTagValue = [NSString stringWithFormat:@"%ld",([tagValue integerValue]-1)];
    _indicateLabel.text = newTagValue;
}

//未读数量改变
- (void)badgeChange:(NSInteger)count{
    _indicateLabel.text = [NSString stringWithFormat:@"%ld",count];
}

@end
